﻿//-----------------------------------------------------------------------
// <copyright file="Package.cs" company="FastNET Services">
//     Copyright (c) 2008-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings
using System;
using System.Collections.Generic;
using FastNET.CleanAct.Common.Repository;
using FastNET.CleanAct.Common.Repository.Business;
using FastNET.CleanAct.Common.Repository.Interfaces.DataInterfaces.ManagementEngine;
using FastNET.CleanAct.Common.Repository.Interfaces.Factories;
using FastNET.CleanAct.Common.Repository.Interfaces.Repositories.ManagementEngine;

#endregion

namespace FastNET.CleanAct.Client.ManagerEngineBusiness.DataObjects
{
    /// <summary>
    /// Business Class representing a Package for the client
    /// </summary>
    public class Package : IdentityBusinessBase<IPackageData, IPackageRepository>
    {
        #region AutoLoaded Property Fields



        #endregion

        #region Public CTor/DTor

        /// <summary>
        /// Initializes a new instance of the Package class
        /// </summary>
        public Package()
            : this(new RepositoryFactory())
        {
            Data.DateCreated = DateTime.UtcNow;
            Data.Version = new Version(1, 0, 0, 0);
        }

        /// <summary>
        /// Initializes a new instance of the Package class
        /// </summary>
        /// <param name="ID">Identity of the data to retrieve</param>
        public Package(Identity ID)
            : this(new RepositoryFactory(), ID)
        {
        }

        #endregion

        #region Internal CTor/DTor

        /// <summary>
        /// Initializes a new instance of the Package class
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <param name="data">Data for backing the business object</param>
        /// <exception cref="ArgumentNullException">Thrown if Factory or Data are null</exception>
        internal Package(IRepositoryFactory factory, IPackageData data)
            : base(factory, data)
        {
        }

        /// <summary>
        /// Initializes a new instance of the Package class
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <exception cref="ArgumentNullException">Thrown if Factory is null</exception>
        internal Package(IRepositoryFactory factory)
            : base(factory)
        {
        }

        /// <summary>
        /// Initializes a new instance of the Package class
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <param name="ID">Identity of the data to retrieve</param>
        /// <exception cref="ArgumentNullException">Thrown if Factory is null</exception>
        internal Package(IRepositoryFactory factory, Identity ID)
            : base(factory, ID)
        {
        }

        #endregion

        #region Overrides of BusinessBase<IActionData,IActionRepository>

        /// <summary>
        /// Method to check whether or not the current object is valid
        /// </summary>
        /// <returns>Boolean based on the validity of the object</returns>
        public override bool IsValid()
        {
            return true;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the date the package was created
        /// </summary>
        public DateTime DateCreated
        {
            get
            {
                return Data.DateCreated;
            }
        }

        /// <summary>
        /// Gets or sets the name of the action
        /// </summary>
        public string Name
        {
            get
            {
                return Data.Name;
            }

            set
            {
                Data.Name = value;
            }
        }

        /// <summary>
        /// Gets or sets the version identifier
        /// </summary>
        public Version Version
        {
            get
            {
                return Data.Version;
            }

            set
            {
                Data.Version = value;
            }
        }

        /// <summary>
        /// Gets or sets the name of the publishing entity
        /// </summary>
        public string Publisher
        {
            get
            {
                return Data.Publisher;
            }

            set
            {
                Data.Publisher = value;
            }
        }

        #endregion

        #region Public Static Factory Methods

        /// <summary>
        /// Retrieves a list of Packages
        /// </summary>
        /// <returns>IList of Package found or null if no items were found</returns>
        public static IList<Package> Retrieve()
        {
            return Retrieve(new RepositoryFactory());
        }

        /// <summary>
        /// Retrieves a package by the name
        /// </summary>
        /// <param name="name">Name of the package to retrieve</param>
        /// <returns>Package of the item found or null if a package was not found</returns>
        public static Package Retrieve(string name)
        {
            return Retrieve(name, new RepositoryFactory());
        }

        /// <summary>
        /// Checks for an existing Package Name in the database
        /// </summary>
        /// <param name="name">Name to check for</param>
        /// <returns>Boolean indicating whether or not the name exists</returns>
        public static bool CheckNameExists(string name)
        {
            return CheckNameExists(name, null);
        }

        /// <summary>
        /// Checks for an existing Package Name in the database
        /// </summary>
        /// <param name="name">Name to check for</param>
        /// <param name="ignoreID">Optional ID to ignore during purposes of renaming</param>
        /// <returns>Boolean indicating whether or not the name exists</returns>
        public static bool CheckNameExists(string name, Identity? ignoreID)
        {
            return CheckNameExists(name, ignoreID, new RepositoryFactory());
        }

        #endregion

        #region Internal Static Factory Methods

        #region Retrieve

        /// <summary>
        /// Retrieves a list of Packages
        /// </summary>
        /// <param name="factory">Factory to use to retrieve data from</param>
        /// <returns>IList of Package found or null if no items were found</returns>
        /// <exception cref="ArgumentNullException">Thrown if factory is null</exception>
        internal static IList<Package> Retrieve(IRepositoryFactory factory)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory", "Factory cannot be null to retrieve data");
            }

            // Try to get a Package
            IList<IPackageData> items = RetrieveRepository(factory).Retrieve();
            IList<Package> returnItems = new List<Package>();

            foreach (IPackageData item in items)
                returnItems.Add(new Package(factory, item));

            return returnItems;
        }

        /// <summary>
        /// Retrieves a package by name
        /// </summary>
        /// <param name="name">Name of the package to retrieve</param>
        /// <param name="factory">Factory to use to retrieve data from</param>
        /// <returns>Package found or null if no item was found</returns>
        /// <exception cref="ArgumentNullException">Thrown if factory is null</exception>
        internal static Package Retrieve(string name, IRepositoryFactory factory)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory", "Factory cannot be null to retrieve data");
            }

            // Try to get a Package
            IPackageData item = RetrieveRepository(factory).Retrieve(name);

            return item != null ? new Package(factory, item) : null;
        }

        #endregion

        /// <summary>
        /// Checks for an existing Package Name in the database
        /// </summary>
        /// <param name="name">Name to check for</param>
        /// <param name="ignoreID">Optional ID to ignore during purposes of renaming</param>
        /// <param name="factory">Factory to use to retrieve data from</param>
        /// <returns>Boolean indicating whether or not the name exists</returns>
        /// <exception cref="ArgumentNullException">Thrown if factory is null</exception>
        internal static bool CheckNameExists(string name, Identity? ignoreID, IRepositoryFactory factory)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory", "Factory cannot be null to retrieve data");
            }

            return RetrieveRepository(factory).CheckExistingName(name, ignoreID);
        }

        #endregion
    }
}